//
//  CZStatusOriginalView.swift
//  WeiBo
//
//  Created by Apple on 16/9/6.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SDWebImage
import YYText

//  原创微博视图
class CZStatusOriginalView: UIView {
    //  原创微博的底部约束
    var originalViewBottomConstraint: Constraint?
    
    //  绑定原创数据
    var statusViewModel: CZStatusViewModel? {
        didSet {
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                headImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            //contentLabel.text = statusViewModel?.status?.text
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            mbrankImageView.image = statusViewModel?.mbrankImage
            sourceLabel.text = statusViewModel?.sourceContent
            timeLabel.text = statusViewModel?.timeContent
            
            //  卸载约束
            originalViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  有配图,显示配图，更新约束，绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
                
            } else {
                //  没有配图, 隐藏配图，更新约束
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }
    
    // MARK:    --懒加载控件
    //  头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 14, textColor: UIColor.darkGray)
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 11, textColor: UIColor.orange)
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 11, textColor: UIColor.gray)
    //  原创微博内容
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 13, textColor: UIColor.gray)
        let label = YYLabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.gray
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: CZStatusPictureView = {
        let view = CZStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        screenNameLabel.text = "小逗比神吐槽"
        timeLabel.text = "刚刚"
        sourceLabel.text = "来源"
        contentLabel.text = "听说下雨天音乐跟辣条更配哟，听说下雨天音乐跟辣条更配哟，听说下雨天音乐跟辣条更配哟，听说下雨天音乐跟辣条更配哟"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //  添加约束
        headImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.top.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            //make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            make.left.equalTo(contentLabel)
        }
    
        //  关键约束， 原创微博的底部约束 = 微博内容的底部约束 + 间距
        self.snp_makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    
        //  点击高亮链接的闭包
        contentLabel.highlightTapAction = { (containerView, text, range, rect)->() in
           
            
            let result = (text.string as NSString).substring(with: range)
            if result.hasPrefix("http") {
                print("我是链接")
            } else {
                print(result)
            }
            
        }
        
    }

}
